package com.easygo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easygo.common.PageUtils;
import com.easygo.common.ResultEnum;
import com.easygo.common.ResultResponse;
import com.easygo.pojo.Content;
import com.easygo.pojo.ContentCategory;
import com.easygo.service.ContentCategoryService;
import com.easygo.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Scope("prototype")
public class ContentController {

    @Autowired
    ContentService contentService;
    @Autowired
    ContentCategoryService contentCategoryService;

    @RequestMapping("/content_page/{pageIndex}/{pageSize}")
    public PageUtils<Content> getContentPage(@PathVariable("pageIndex") Long pageIndex, @PathVariable("pageSize") Long pageSize) {

        System.out.println("pageIndex=" + pageIndex);
        System.out.println("pageSize=" + pageSize);

        //MyBatisPlus的分页插件
        IPage<Content> ipage = new Page<Content>(pageIndex, pageSize);
        IPage<Content> page = contentService.page(ipage, new QueryWrapper<Content>().eq("status", 0).eq("del", 0));

        long total = page.getTotal();
        List<Content> records = page.getRecords();
        PageUtils<Content> pageUtils = new PageUtils<Content>(pageIndex, pageSize, total, records);
        return pageUtils;
    }

    @PostMapping("/content_add")
    public ResultResponse content_add(@RequestBody Content content) {
        boolean b = contentService.save(content);
        if (b) {
            return ResultResponse.success(ResultEnum.INSERT_SUCCESS);
        } else {
            return ResultResponse.success(ResultEnum.INSERT_FAIL);
        }
    }

}
